#include <cstdio>
#include <cstring>
#include "mysql.h"

using namespace std;

static char query[1024];

int main(){
    MYSQL conn;     //mysql连接
    MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
    MYSQL_ROW row; //一个行数据的类型安全(type-safe)的表示
    //char *query;  //查询语句
    int t,r;
    mysql_init(&conn);

    if (!mysql_real_connect(&conn,"127.0.0.1", "root", "123456", "goj",3306,NULL,0))
    {
        printf( "Error connecting to database: %s\n",mysql_error(&conn));
    }  else
        printf("Connected\n");

    /*query="SET CHARACTER SET UTF8"; //设置编码
    t=mysql_real_query(&conn,query,(unsigned int)strlen(query));
    if(t)
    {
        printf("编码设置失败\n");
    }*/
    sprintf(query,"SELECT solution_id FROM solution WHERE language in (%s) and result<2 ORDER BY solution_id  limit %d",
            "0,1,2,3,4,5,6",  4 );
    t=mysql_real_query(&conn,query,(unsigned int)strlen(query));
    if(t)
    {
        printf("执行查询时出现异常: %s",mysql_error(&conn));

    }else
        printf("[%s] 构建成功 \n",query);
    res=mysql_store_result(&conn);
    while(row=mysql_fetch_row(res))
    {
        for(t=0;t<mysql_num_fields(res);t++)
        {
            printf("%s\t",row[t]);
        }
        printf("\n");
    }
    mysql_free_result(res);

    return 0;
}